﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common.Xml;

namespace RichECMS.ServerPages.SysManage
{
    public partial class WebSetting : Util.BasePage.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadSetting();
            }
        }

        /// <summary>
        /// 加载设置
        /// </summary>
        protected void LoadSetting()
        {
            //自定义路径
            if (GetPropertyValue("ParentPath", "Enable"))
            {
                RbtnCustomPathYes.Checked = true;
                RbtnCustomPathNo.Checked = false;
            }
            else
            {
                RbtnCustomPathYes.Checked = false;
                RbtnCustomPathNo.Checked = true;
            }

            //短路径路径
            if (GetPropertyValue("ShortPath", "Enable"))
            {
                RbtnShortPathYes.Checked = true;
                RbtnShortPathNo.Checked = false;
            }
            else
            {
                RbtnShortPathYes.Checked = false;
                RbtnShortPathNo.Checked = true;
            }

            //文章页启用栏目标识
            if (GetPropertyValue("ParentPath", "EnableSectionID"))
            {
                RbtnCustiomPageNameYes.Checked = true;
                RbtnCustiomPageNameNo.Checked = false;
            }
            else
            {
                RbtnCustiomPageNameYes.Checked = false;
                RbtnCustiomPageNameNo.Checked = true;
            }

            //编辑完成时自动生成详情页面
            if (GetPropertyValue("SyncPageBySave", "Enable"))
            {
                RbtnAutoCreatePageYes.Checked = true;
                RbtnAutoCreatePageNo.Checked = false;
            }
            else
            {
                RbtnAutoCreatePageYes.Checked = false;
                RbtnAutoCreatePageNo.Checked = true;
            }

            //栏目引用
            if (GetPropertyValue("SectionRefer", "Enable"))
            {
                RbtnSectionRefYes.Checked = true;
                RbtnSectionRefNo.Checked = false;
            }
            else
            {
                RbtnSectionRefYes.Checked = false;
                RbtnSectionRefNo.Checked = true;
            }

            //服务器端包含(Shtml)
            if (GetPropertyValue("PageType", "EnabledInclude"))
            {
                RbtnServerIncludeYes.Checked = true;
                RbtnServerIncludeNo.Checked = false;
            }
            else
            {
                RbtnServerIncludeYes.Checked = false;
                RbtnServerIncludeNo.Checked = true;
            }

            if (RbtnServerIncludeYes.Checked)
            {
                divIssueType.Visible = false;
            }
            else
            {
                divIssueType.Visible = true;
            }

            //发布模式
            if (GetPropertyValue("IssueType", "CommonIssue"))
            {
                RbtnIssueTypeCommonYes.Checked = true;
                RbtnIssueTypeCommonNo.Checked = false;
            }
            else
            {
                RbtnIssueTypeCommonYes.Checked = false;
                RbtnIssueTypeCommonNo.Checked = true;
            }
        }

        /// <summary>
        /// 通用读取设置
        /// </summary>
        /// <param name="strNodeName"></param>
        /// <param name="strElementPropertyName"></param>
        /// <returns></returns>
        protected bool GetPropertyValue(string strNodeName, string strElementPropertyName)
        {
            bool result = false;
            XMLProcess objXMLProcess = new XMLProcess("Setting\\GlobalSet.config");
            string getConfig = objXMLProcess.ReadAttribute("/configuration/GlobalSet/" + strNodeName, strElementPropertyName);
            if (getConfig.Trim().ToLower() == "true")
            {
                result = true;
            }
            return result;
        }

        /// <summary>
        /// 更新读取设置
        /// </summary>
        /// <param name="strNodeName"></param>
        /// <param name="strElementPropertyName"></param>
        /// <returns></returns>
        protected void SyncPropertyValue(string strNodeName, string strElementPropertyName, bool bolValue)
        {
            string strSyncValue = "True";
            XMLProcess objXMLProcess = new XMLProcess("Setting\\GlobalSet.config");
            if (bolValue == true)
            {
                strSyncValue = "True";
            }
            else
            {
                strSyncValue = "False";
            }
            objXMLProcess.WriteAttribute("/configuration/GlobalSet/" + strNodeName, strElementPropertyName, strSyncValue);
        }

        /// <summary>
        /// 保存设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnSave(object sender, EventArgs e)
        {
            try
            {
                //自定义路径
                SyncPropertyValue("ParentPath", "Enable", RbtnCustomPathYes.Checked);
                SyncPropertyValue("ShortPath", "Enable", RbtnCustomPathYes.Checked);
                SyncPropertyValue("ParentPath", "EnableSectionID", RbtnCustiomPageNameYes.Checked);
                SyncPropertyValue("SyncPageBySave", "Enable", RbtnAutoCreatePageYes.Checked);
                SyncPropertyValue("SectionRefer", "Enable", RbtnSectionRefYes.Checked);
                SyncPropertyValue("PageType", "EnabledInclude", RbtnServerIncludeYes.Checked);
                SyncPropertyValue("IssueType", "CommonIssue", RbtnIssueTypeCommonYes.Checked);
                Alert("设置已经保存，重新登陆后生效");
            }
            catch (Exception ex)
            {
                Alert(ex.Message.Replace("\r\n", ""));
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void IssueTypeChange(object sender, EventArgs e)
        {
            if (RbtnServerIncludeYes.Checked)
            {
                divIssueType.Visible = false;
            }
            else
            {
                divIssueType.Visible = true;
            }
        }
    }
}